from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.contrib import messages
from foodApp.models import *
from utils.query import *
from utils.getPublicData import *
from utils.getChartData import *
from predict.index import *


# Create your views here.
def index(request):
    uname = request.session.get('username')
    userInfo = User.objects.get(username=uname)
    max_value,max_name,max_comment,most_name = getIndexData()
    xBarData,y1BarData,y2BarData,pieData,funnelData = getIndexChartData()
    return render(request, 'index.html',{
        'userInfo':userInfo,
        'max_value':max_value,
        'max_name':max_name,
        'max_comment':max_comment,
        'most_name':most_name,
        'xBarData':xBarData,
        'y1BarData':y1BarData,
        'y2BarData':y2BarData,
        'pieData':pieData,
        'funnelData':funnelData
    })

def selfInfo(request):
    uname = request.session.get('username')
    userInfo = User.objects.get(username=uname)
    if request.method == "POST":
        res = changePwd(request.POST,userInfo)
        if res != None:
            messages.error(request, res)
            return HttpResponseRedirect('/foodApp/selfInfo/')
        userInfo = User.objects.get(username=uname)
        messages.success(request,'修改成功')
    return render(request, 'selfInfo.html',{
        'userInfo':userInfo,
    })
def table(request):
    uname = request.session.get('username')
    userInfo = User.objects.get(username=uname)
    def map_fn(item):
        item_list = list(item)
        item_list[6] = item_list[6] / 10
        item_list.append(item[6] * 2)
        return item_list
    if request.method == "POST":
        keywords = '%' + request.POST.get('title') + '%'
        tableData = querys('select * from fooddata where title like %s',[keywords],'select')
        tableData = map(map_fn,tableData)
    else:
        tableData = querys('select * from fooddata', [], 'select')[:100]
        tableData = map(map_fn, tableData)
    return render(request, 'table.html',{
        'userInfo':userInfo,
        'tableData':tableData
    })

def typeChart(request):
    uname = request.session.get('username')
    userInfo = User.objects.get(username=uname)
    lineXData,lineY1Data,lineY2Data,lineY3Data,pieData,barXData,barYData = getTypeChartData()
    return render(request, 'typeChart.html', {
        'userInfo': userInfo,
        'lineXData':lineXData,
        'lineY1Data':lineY1Data,
        'lineY2Data':lineY2Data,
        'lineY3Data':lineY3Data,
        'pieData':pieData,
        'barXData':barXData,
        'barYData':barYData
    })

def priceChart(request):
    uname = request.session.get('username')
    userInfo = User.objects.get(username=uname)
    leftXData,leftYData,rightXData,rightYData,circleData = getPriceChartData()
    return render(request, 'priceChart.html', {
        'userInfo': userInfo,
        'leftXData':leftXData,
        'leftYData': leftYData,
        'rightXData': rightXData,
        'rightYData': rightYData,
        'circleData': circleData
    })

def rateChart(request):
    uname = request.session.get('username')
    userInfo = User.objects.get(username=uname)
    xData, yData, roseData,realData = getRateChartData()
    return render(request, 'rateChart.html', {
        'userInfo': userInfo,
        'xData': xData,
        'yData': yData,
        'roseData': roseData,
        'realData': realData,
    })

def areaChart(request):
    uname = request.session.get('username')
    userInfo = User.objects.get(username=uname)

    defaultNum = '最大值'
    if request.method == 'POST':
        defaultNum = request.POST.get('num')
        mapData,xDate,yData = getAreaChartData(defaultNum)
        return render(request, 'areaChart.html', {
            'userInfo': userInfo,
            'mapData': mapData,
            'xDate': xDate,
            'yData': yData
        })
    mapData,xDate,yData = getAreaChartData(defaultNum)
    return render(request, 'areaChart.html', {
        'userInfo': userInfo,
        'mapData': mapData,
        'xDate': xDate,
        'yData': yData
    })

def foodCloud(request):
    uname = request.session.get('username')
    userInfo = User.objects.get(username=uname)

    return render(request, 'foodCloud.html', {
        'userInfo': userInfo,

    })

def preChart(request):
    uname = request.session.get('username')
    userInfo = User.objects.get(username=uname)
    typeList = getPreSelect()
    if request.method == 'POST':
        price = request.POST.get('price')
        type = request.POST.get('type')
        taste = request.POST.get('taste')
        envs = request.POST.get('envs')
        serve = request.POST.get('serve')
        singleDataExample = {
            'avgPrice': price,
            'type': type,
            'tasterate': taste,
            'envsrate': envs,
            'serverate': serve
        };

        predicted_value = predict_start(singleDataExample)
        predicted_value = int(round(predicted_value / 10,0))
        return render(request, 'preChart.html', {
            'userInfo': userInfo,
            'typeList': typeList,
            'predicted_value':predicted_value
        })
    return render(request, 'preChart.html', {
        'userInfo': userInfo,
        'typeList':typeList,
    })


def logOut(request):
    request.session.clear()
    return HttpResponseRedirect('login')

def login(request):
    if request.method == 'GET':
        return render(request, 'login.html',{})
    else:
        loginUsername = request.POST.get('loginUsername')
        loginPassword = request.POST.get('loginPassword')
        print(loginUsername,loginPassword)
        try:
            user = User.objects.get(username=loginUsername, password=loginPassword)
            request.session['username'] = loginUsername
            return HttpResponseRedirect('/foodApp/index/')
        except:
            message = '用户名或密码错误'
            messages.error(request, message)
            return HttpResponseRedirect('/foodApp/login/')

def register(request):
    if request.method == 'GET':
        return render(request, 'register.html',{})
    else:
        username = request.POST.get('username')
        password = request.POST.get('password')
        ckPassword = request.POST.get('ckPassword')
        try:
            User.objects.get(username=username)
            message = '用户已存在'
            messages.error(request, message)
            return HttpResponseRedirect('/foodApp/register/')
        except:
            if not username or not password or not ckPassword:
                message = '请输入完整信息'
                messages.error(request, message)
                return HttpResponseRedirect('/foodApp/register/')
            elif password != ckPassword:
                message = '两次密码不一致'
                messages.error(request, message)
                return HttpResponseRedirect('/foodApp/register/')
            else:
                User.objects.create(username=username,password=password)
                message.success(request,'注册成功')
                return HttpResponseRedirect('/foodApp/login/')